<template>
  <a-modal v-model:open="open" title="回复" width="800px" @ok="handleOk" @cancel="handleCancel">
    <a-form ref="formRef" :model="formState" :label-col="{ style: { width: '80px' } }" style="margin-top: 16px">
      <a-form-item label="回复内容" name="answer" :rules="[{ required: true, message: '请输入回复' }]">
        <a-textarea v-model:value="formState.answer" placeholder="请输入" :rows="5"></a-textarea>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { fetchMessageReply } from '@/api/message'
import { message } from 'ant-design-vue'

const emits = defineEmits(['refresh'])

const open = ref(false)
const handleOk = () => {
  formRef.value.validate().then(async () => {
    const res = await fetchMessageReply(formState.value)
    if (res.data.code === '200') {
      message.success('留言成功')
      emits('refresh')
      open.value = false
    } else {
      message.error(res.data.message)
    }
  })
}
const handleCancel = () => {
  Object.keys(formState.value).forEach((key) => {
    formState.value[key] = undefined
  })
  open.value = false
}
const openModal = (record: any) => {
  Object.keys(formState.value).forEach((key) => {
    formState.value[key] = record[key]
  })
  open.value = true
}

const formRef = ref()
const formState = ref<any>({
  id: undefined,
  answer: undefined,
  question: undefined,
  label_top_name: undefined,
  label_second_name: undefined,
  label_top: undefined,
  label_second: undefined,
})

const firstLabelList = ref<any[]>([])

defineExpose({ openModal })
</script>

<style scoped lang="scss"></style>
